package com.example.book_libapp;

import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.database.library.DatabaseHandler;
import com.example.database.library.UserFunctions;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BookHistory extends Activity {

	private EditText inputDatefrom;
	private EditText inputDateto;
	
	private Button btnhistory;
	private Button btndatefrom;
	private Button btndateto;

	static final String TAG_DATEFROM = "date_from";
	static final String TAG_DATETO = "date_to";
	
	static final int DATE_DIALOG_ID = 0;
	
	boolean dalogstat = true;
	
	
	private TextView ErrorMsg;
//	private static String api_key = "nmc-android";

	// JSON Response node names
//	private static String RegisterURL = "http://bookstore-sf14.l:8080/api/"
//			+ api_key + "/auth/signup.json";
	// private static String KEY_CREATED_AT = "created_at";

	public void onSearch(View v) {
		onSearchRequested();
	}

	public void onHome(View v) {
		return2Home(this);
	}

	public void return2Home(Context context) {
		final Intent intent = new Intent(context, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_history);
	
		inputDatefrom = (EditText) findViewById(R.id.InputDatefrom);
		inputDateto = (EditText) findViewById(R.id.InputDateto);
		
		btndatefrom = (Button) findViewById(R.id.BtnDatefrom);
		btndateto = (Button) findViewById(R.id.BtnDateto);
		btnhistory = (Button) findViewById(R.id.Btnhistory);
		
		ErrorMsg = (TextView) findViewById(R.id.login_error);

		//checkserver = new CheckConnectServer();
//
		btndatefrom.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				dalogstat = true;
				showDialog(DATE_DIALOG_ID);
			}
		});

		// Register button Click Event
		btndateto.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dalogstat = false;
				showDialog(DATE_DIALOG_ID);
			}
		});
		btnhistory.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				String inputDatefromtext = inputDatefrom.getText().toString();
				String inputDatetotext = inputDateto.getText().toString();

				Intent i = new Intent(getApplicationContext(),
						BookHistoryView.class);
				i.putExtra(TAG_DATEFROM, inputDatefromtext);
				i.putExtra(TAG_DATETO, inputDatetotext);
				startActivity(i);
			}
		});

	}
	
	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		int cyear = c.get(Calendar.YEAR);
		int cmonth = c.get(Calendar.MONTH);
		int cday = c.get(Calendar.DAY_OF_MONTH);
		return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
						cday);
		}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		// onDateSet method
		public void onDateSet(DatePicker view, int year, int monthOfYear,
		int dayOfMonth) {
			String date_selected = String.valueOf(year) + "-" + String.valueOf(monthOfYear + 1) +"-"
					+ String.valueOf(dayOfMonth);
			if (dalogstat) { 

				inputDatefrom.setText(date_selected);
			} else {
				inputDateto.setText(date_selected);
			}
			
		
		}
		};
	
}
